//
//  XNEducationViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/14.
//

#import "XNEducationViewController.h"
#import "XNEducationBillInfoModel.h"
#import "XNEducationApproveRequest.h"
#import "XNEducationBillInfoRequest.h"


@interface XNEducationViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNEducationBillInfoModel *infoModel;

@end

@implementation XNEducationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNEducationBillInfoRequest *infoRequest = [[XNEducationBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNEducationBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNEducationBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNEducationApproveRequest *approveRequest = [[XNEducationApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNEducationApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNEducationApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}

- (void)creatTableData
{
    
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    itemModel.showOverImage = self.infoModel.budgetItemObject.remainApplyMoney < 0;
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"制单部门： %@",self.infoModel.createUserObject.departmentName],
                              [NSString stringWithFormat:@"职务： %@",self.infoModel.createUserObject.positionName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"个人信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请人: %@",self.infoModel.applicantObject.userName],
                               [NSString stringWithFormat:@"申请部门： %@",self.infoModel.applicantObject.departmentName],
                               [NSString stringWithFormat:@"职务： %@",self.infoModel.applicantObject.positionName],
                               [NSString stringWithFormat:@"工号： %@",self.infoModel.applicant],
                               [NSString stringWithFormat:@"性别： %@",self.infoModel.applicantObject.genderStr],
                               [NSString stringWithFormat:@"出生年月： %@",self.infoModel.applicantObject.birthdayStr],
                               [NSString stringWithFormat:@"学历： %@",self.infoModel.applicantObject.educationStr.length > 0 ? self.infoModel.applicantObject.educationStr : @""],
                               [NSString stringWithFormat:@"入职日期： %@",self.infoModel.applicantObject.inDateStr],
                               nil];
    itemModel2.showItemArray = showItemArray2;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    XNTableShowDataModel *showModel3 = [[XNTableShowDataModel alloc] init];
    showModel3.showType = XNShowType_Normal;
    showModel3.title = @"培训信息";
    XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray3 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"培训日期: %@至%@ 共 %ld天",self.infoModel.cultivateStartTimeStr,self.infoModel.cultivateEndTimeStr,self.infoModel.cultivateDay],
                               [NSString stringWithFormat:@"培训地点： %@",self.infoModel.cultivateAddress],
                               [NSString stringWithFormat:@"培训内容： %@",self.infoModel.cultivateContent],
                               nil];
    itemModel3.showItemArray = showItemArray3;
    [showModel3.showDataArray addObject:itemModel3];
    [self.showArray addObject:showModel3];
    
    XNTableShowDataModel *showModel4 = [[XNTableShowDataModel alloc] init];
    showModel4.showType = XNShowType_Normal;
    showModel4.title = @"同行人员";
    [self.infoModel.itemList enumerateObjectsUsingBlock:^(XNEducationItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        NSArray *showItemArray4 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"人员： %@",obj.educationUserObject.userName],
                                   [NSString stringWithFormat:@"部门名称： %@",obj.educationUserObject.departmentName],
                                   [NSString stringWithFormat:@"岗位： %@",obj.educationUserObject.positionName],
                                   [NSString stringWithFormat:@"联系电话： %@",obj.educationUserObject.telephone],
                                   nil];
        temp.showItemArray = showItemArray4;
        [showModel4.showDataArray addObject:temp];
    }];
    [self.showArray addObject:showModel4];
    
    
    
    XNTableShowDataModel *reimbursementModel = [[XNTableShowDataModel alloc] init];
    reimbursementModel.showType = XNShowType_Normal;
    reimbursementModel.title = @"培训费用";
    reimbursementModel.showWarning = YES;
    reimbursementModel.warningStr = @"培训申请单只能报销培训费，培训过程发生差旅、其它费用必须另行申请报销！";
    XNShowItemModel *temp = [[XNShowItemModel alloc] init];
    NSArray *showItemArray5 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"培训课程费： %.3f",self.infoModel.cultivateCost],
                               [NSString stringWithFormat:@"人数： %ld",self.infoModel.cultivatePeopleCount],
                               [NSString stringWithFormat:@"总培训费： %.3f",self.infoModel.cultivateTotalCost],
                               nil];
    temp.showItemArray = showItemArray5;
    [reimbursementModel.showDataArray addObject:temp];
    [self.showArray addObject:reimbursementModel];
    
    XNTableShowDataModel *showModel8 = [[XNTableShowDataModel alloc] init];
    showModel8.showType = XNShowType_Normal;
    showModel8.title = @"交通费";
    XNShowItemModel *itemModel8 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray8 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"人数： %ld",self.infoModel.transportPeopleCount],
                                       [NSString stringWithFormat:@"交通费用合计：%.3f",self.infoModel.transportCost],
                                       nil];
    itemModel8.showItemArray = showItemArray8;
    [showModel8.showDataArray addObject:itemModel8];
    [self.showArray addObject:showModel8];
    
    XNTableShowDataModel *showModel6 = [[XNTableShowDataModel alloc] init];
    showModel6.showType = XNShowType_Normal;
    showModel6.title = @"其它";
    XNShowItemModel *itemModel6 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray6 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"其它： %.3f",self.infoModel.otherCost],
                                       [NSString stringWithFormat:@"本次培训费总计：%.3f",self.infoModel.totalCost],
                                        [NSString stringWithFormat:@"是否暂支：%@",self.infoModel.isTemp ? @"是":@"否"],
                               [NSString stringWithFormat:@"备注：%@",self.infoModel.remark.length > 0 ? self.infoModel.remark : @""],
                                       nil];
    itemModel6.showItemArray = showItemArray6;
    [showModel6.showDataArray addObject:itemModel6];
    [self.showArray addObject:showModel6];
    
    
    if (self.infoModel.isTemp) {
        XNTableShowDataModel *advanceModel = [[XNTableShowDataModel alloc] init];
        advanceModel.showType = XNShowType_Normal;
        advanceModel.title = @"暂支单";
        XNShowItemModel *advanceItemModel = [[XNShowItemModel alloc] init];
        NSArray *advanceShowItemArray = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"暂支事由： %@",self.infoModel.tempReason],
                                   [NSString stringWithFormat:@"暂支金额： %.3f",self.infoModel.tempCost],
                                   [NSString stringWithFormat:@"预计归还日期： %@",self.infoModel.returnDateStr],
                                   nil];
        advanceItemModel.showItemArray = advanceShowItemArray;
        [advanceModel.showDataArray addObject:advanceItemModel];
        [self.showArray addObject:advanceModel];
    }
    
    XNTableShowDataModel *showModel7 = [[XNTableShowDataModel alloc] init];
    showModel7.showType = XNShowType_Normal;
    showModel7.title = @"部门培训费";
    XNShowItemModel *itemModel7 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray7 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"（%@）预算总金额:%.3f",self.infoModel.budgetItemObject.budgetInfo,self.infoModel.budgetItemObject.allMoney],
                               [NSString stringWithFormat:@"已报销： %.3f",self.infoModel.budgetItemObject.reimbursedMoney],
                               [NSString stringWithFormat:@"报销中： %.3f",self.infoModel.budgetItemObject.reimbursingMoney],
                               [NSString stringWithFormat:@"剩余可报销： %.3f",self.infoModel.budgetItemObject.remainReimburseMoney],
                               [NSString stringWithFormat:@"已申请未报销： %.3f",self.infoModel.budgetItemObject.applyedMoney],
                               [NSString stringWithFormat:@"在申请未报销： %.3f",self.infoModel.budgetItemObject.applyingMoney],
                               [NSString stringWithFormat:@"剩余可申请： %.3f",self.infoModel.budgetItemObject.remainApplyMoney],
                               nil];
    itemModel7.showItemArray = showItemArray7;
    [showModel7.showDataArray addObject:itemModel7];
    [self.showArray addObject:showModel7];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}

@end
